/**************************************************
 * schedule日历js
 * 
 * * 根据class属性返回对象
 * * 2008-6-22
 * **************************************************
 * * msn:danxinju@hotmail.com
 * * author:淡新举
 ***************************************************/
var getElementsByClassName = function(className) {
	var alls = document.getElementsByTagName("*");
	var rets = new Array();

	for ( var i = 0; i < alls.length; i++) {
		if (alls[i].className == className) {
			rets.push(alls[i]);
		}
	}

	return rets;
};

var Calendar = {
	model : function() {
	},
	controller : function() {
	},
	view : function() {
	}
};

//视图层
Calendar.view = function() {
	this.currDate = new Date();
	this.tds = null;
	this.days = null;

	this.backNode = null;
	this.disableDays = new Array();
};

//根据年份返回每月天数
Calendar.view.getMonthDays = function(year) {
	var feb = (year % 4 == 0) ? 29 : 28;
	return new Array(31, feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
};

//初始化
Calendar.view.prototype.init = function() {
	this.setCurrMonth(new Date().getFullYear(), new Date().getMonth());
	this.addEventForTd();
};

//为单元格注册事件
Calendar.view.prototype.addEventForTd = function() {
	for ( var i = 0; i < this.tds.length; i++) {
		this.tds[i].onclick = function() {
			var arr = this.getAttribute("dateValue").split("-");
			//alert(arr);
			var scheduledDate = arr[0] + "-" + (parseInt(arr[1]) + 1) + "-"+ arr[2];
			document.getElementById("addSecheduleDiv").style.display = "block";
			//显示选中单元格的日期在lable
			document.getElementById("lblDate").innerHTML = scheduledDate;
			
			document.getElementById("txtDate").value = scheduledDate;
		};
	}
};

//设定当前版面
Calendar.view.prototype.setCurrMonth = function(y, m) {

	this.currDate.setFullYear(y);
	this.currDate.setMonth(m);
	this.currDate.setDate(1);

	this.loadDaysByMonth(y, m);
};

//标示当前天
Calendar.view.prototype.markCurrDate = function(bDay, eDay) {
	var temp = new Date();
	if (this.currDate.getFullYear() == temp.getFullYear()
			&& this.currDate.getMonth() == temp.getMonth()) {
		for ( var i = bDay; i < eDay; i++) {
			if (this.tds[i].getAttribute("dateValue").split("-")[2] == temp
					.getDate()) {
				if (this.backNode) {
					this.backNode.className = "";
				}
				this.tds[i].className = "currDay";
				this.backNode = this.tds[i];
				return false;
			}
		}
	}
};

//复位版面状态
Calendar.view.prototype.reInState = function() {
	this.tds[35].parentNode.style.display = "none";
	if (this.backNode) {
		this.backNode.className = "";
	}
	for ( var i = 0; i < this.disableDays.length; i++) {
		this.disableDays[i].className = "dayStyle";
	}
	this.disableDays.length = 0;
};

//根据年月加载当前视图
Calendar.view.prototype.loadDaysByMonth = function(y, m) {
	y = parseInt(y), m = parseInt(m);
	this.reInState(); //复位版面状态

	//参数定位
	var beginDay = this.currDate.getDay();
	var _m = (m == 0) ? 11 : (m - 1);
	var m_ = (m == 11) ? 0 : (m + 1);
	var _y = (m == 0) ? (y - 1) : y;
	var y_ = (m == 11) ? (y + 1) : y;

	var prevMonthDays = Calendar.view.getMonthDays(_y)[_m];
	var currMonthDays = Calendar.view.getMonthDays(y)[m];
	var prevFlag = prevMonthDays - beginDay + 1, currFlag = 1, nextFlag = 1;

	//加载上月信息
	for ( var i = 0; i < beginDay; i++) {
		this.tds[i].setAttribute("dateValue", _y + "-" + _m + "-" + prevFlag);
		this.days[i].innerHTML = prevFlag;
		this.days[i].className = "dayStyle disableText";
		this.disableDays.push(this.days[i]);
		prevFlag++;
	}

	//加载当月信息
	for ( var i = beginDay; i < currMonthDays + beginDay; i++) {
		this.tds[i].setAttribute("dateValue", y + "-" + m + "-" + currFlag);
		this.days[i].innerHTML = currFlag;
		currFlag++;
	}

	//加载下月信息
	for ( var i = currMonthDays + beginDay; i < this.days.length; i++) {
		this.tds[i].setAttribute("dateValue", y_ + "-" + m_ + "-" + nextFlag);
		this.days[i].innerHTML = nextFlag;
		this.days[i].className = "dayStyle disableText";
		this.disableDays.push(this.days[i]);
		nextFlag++;
	}

	//若当月数据显示到第7行，那么显示它
	if (this.tds[35].getAttribute("dateValue")) {
		if (this.tds[35].getAttribute("dateValue").split("-")[2] > 20) {
			this.tds[35].parentNode.style.display = "";
		}
	}

	//标示当前天
	this.markCurrDate(beginDay, currMonthDays + beginDay);
};

window.onload = function() {
	view = new Calendar.view();
	view.tds = document.getElementById("render").getElementsByTagName("td");
	view.days = getElementsByClassName("dayStyle");
	view.init();

	document.getElementById("testButton").onclick = function() {
		view.setCurrMonth(document.getElementById("testYear").value, document
				.getElementById("testMonth").value);
		var testYear = document.getElementById("testYear").value;
		var testMonth = document.getElementById("testMonth").value;
		var month = Number(testMonth) + 1;
		document.getElementById("nowDate").innerHTML = testYear + "  年  "
				+ month + "  月  ";
	};
};



function hiddenDiv(){
	document.getElementById("addSecheduleDiv").style.display="none";
}

function checkContent(){
	var tkContent=document.getElementById("tkContent").value;
	if(tkContent ==""){
		document.getElementById("addScheduleBtn").disabled=true;
	}else{
		document.getElementById("addScheduleBtn").disabled=false;
	}
}
